package portlet.dscbttmoinhat.backing;

import javax.faces.context.FacesContext;

import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequest;
import javax.portlet.WindowState;

import portlet.dscbttmoinhat.DscbttMoinhat;
import portlet.dscbttmoinhat.resource.DscbttMoinhatBundle;

public class DscbttMoinhatBacking {
    private String mPortletTitle;
    private String mLoaiDonVi;


    public void setLoaiDonVi(String loaiDonVi) {
        this.mLoaiDonVi = loaiDonVi;
    }

    public String getLoaiDonVi() {
        if (mLoaiDonVi == null) {
            PortletRequest request = (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            PortletPreferences preferences = request.getPreferences();
            mLoaiDonVi =
                    preferences.getValue(DscbttMoinhat.LOAIDONVI_KEY, null);
        }
        return mLoaiDonVi;
    }

    public void setPortletTitle(String portletTitle) {
        mPortletTitle = portletTitle;
    }
    public String getPortletTitle() {
        if (mPortletTitle == null) {
            PortletRequest request = (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            PortletPreferences preferences = request.getPreferences();
            mPortletTitle =
                    preferences.getValue(DscbttMoinhat.PORTLETTITLE_KEY, getResource(DscbttMoinhatBundle.PORTLETTITLE));
        }
        return mPortletTitle;
    }

    public String getPortletTitleLabel() {
        String resource = getResource(DscbttMoinhatBundle.PORTLETTITLE);
        return resource;
    }

    public String applyAction() throws Exception {
        PortletRequest request = (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        PortletPreferences preferences = request.getPreferences();
        preferences.setValues(DscbttMoinhat.PORTLETTITLE_KEY, mPortletTitle.split(","));
        if(mLoaiDonVi!=null&&mLoaiDonVi.isEmpty()==false){
        preferences.setValue(DscbttMoinhat.LOAIDONVI_KEY, mLoaiDonVi);
        }
        preferences.store();
        return null;
    }

    public String getApplyLabel() {
        String resource = getResource(DscbttMoinhatBundle.APPLY_LABEL);
        return resource;
    }

    public String okAction() throws Exception {
        applyAction();
        ActionResponse response = (ActionResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
        response.setPortletMode(PortletMode.VIEW);
        response.setWindowState(WindowState.NORMAL);
        return null;
    }

    public String getOkLabel() {
        String resource = getResource(DscbttMoinhatBundle.OK_LABEL);
        return resource;
    }

    private String getResource(String resourceKey) {
        PortletRequest request = (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        PortletConfig config = (PortletConfig)request.getAttribute("javax.portlet.config");
        return config.getResourceBundle(request.getLocale()).getString(resourceKey);
    }
}
